﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace DLL.UserGroup
{
	/// <summary>
	/// 用户组视图控制类
	/// </summary>
	/// <![CDATA[2014-8-7 17:52:59]]>
	public class View : DLL.ShowLayer.FormView
	{
		private Component clsthisComp;
		private Column.Component clsColumnComp;
		private WebColumn.Component clsWebColumnComp;
		private Config clsConfig = new Config();

		#region 添加、修改

		private TextBox tbUserGroupName = new TextBox();
		private CheckBoxList cblColumn = new CheckBoxList();		// 所有栏目
		private CheckBoxList cblWebColumn = new CheckBoxList();		// 所有前台栏目
		private DropDownList ddlQueryRight = new DropDownList();		// 查询权限

		#endregion 添加、修改

		#region 查询
		private TextBox tbQueryUserGroupName = new TextBox();		// 用户组名
		#endregion 查询

		public View(System.Web.UI.Page page)
			: base(page)
		{
			clsthisComp = new Component(page);
			clsColumnComp = new Column.Component(page);
			clsWebColumnComp = new WebColumn.Component(page);

			clsComp = clsthisComp;

			clsControls.SetAllColumnsToCheckBoxList(cblColumn, "");
			cblColumn.RepeatLayout = RepeatLayout.Table;
			cblColumn.RepeatColumns = 3;
			cblColumn.CssClass = "userGroupColumns";
			clsControls.SetNoInheritWebColumnsToCheckBoxList(cblWebColumn, "");
			cblWebColumn.RepeatLayout = RepeatLayout.Table;
			cblWebColumn.RepeatColumns = 3;
			cblWebColumn.CssClass = "userGroupColumns";
			clsControls.ShowQueryRight(ddlQueryRight);
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("用户组名", "TextBox", tbUserGroupName, "string", "strUserGroupName"));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("栏目", "CheckBoxList", cblColumn, "", ""));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("前台栏目", "CheckBoxList", cblWebColumn, "", ""));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("查询权限", "DropDownList", ddlQueryRight, "int", "nQueryRight"));

			listQuery.Add(new TonyNet4V1.Structs.TitleControlEntity("用户组名", "TextBox", tbQueryUserGroupName, "string", "strUserGroupName"));
		}

		#region 类型转换

		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}

		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}

		protected override TonyNet4V1.ComponentBase GetComponent()
		{
			return clsthisComp;
		}

		#endregion 类型转换
		/// <summary>
		/// 控件给实体赋值
		/// </summary>
		/// <![CDATA[2014-8-10 11:33:19]]>
		/// <param name="clsEntity"></param>
		/// <param name="listForm"></param>
		protected override void ControlToEntity(TonyNet4V1.EntityBase clsEntity, List<TonyNet4V1.Structs.TitleControlEntity> listForm)
		{
			//base.ControlToEntity(clsEntity, listForm);

			string strColumnIDs = "";
			string strWebColumnIDs = "";

			base.ControlToEntity(clsEntity, listForm);

			for (int i = 0; i < cblColumn.Items.Count; i++)
			{
				if (cblColumn.Items[i].Selected == true)
				{
					strColumnIDs = strColumnIDs + cblColumn.Items[i].Value.Trim() + ", ";
					SelectFather(Convert.ToInt32(cblColumn.Items[i].Value), ref strColumnIDs);
				}
			}

			for (int i = 0; i < cblWebColumn.Items.Count; i++)
			{
				if (cblWebColumn.Items[i].Selected == true)
				{
					strWebColumnIDs = strWebColumnIDs + cblWebColumn.Items[i].Value.Trim() + ", ";
					SelectWebFather(Convert.ToInt32(cblWebColumn.Items[i].Value), ref strWebColumnIDs);
				}
			}

			strColumnIDs = TonyNet4V1.CommonFunction.ExceptString(", ", strColumnIDs);
			strWebColumnIDs = TonyNet4V1.CommonFunction.ExceptString(", ", strWebColumnIDs);

			((Entity)clsEntity).ColumnIDs = strColumnIDs;
			((Entity)clsEntity).WebColumnIDs = strWebColumnIDs;
		}
		protected override void EntityToControl(TonyNet4V1.EntityBase clsEntity, List<TonyNet4V1.Structs.TitleControlEntity> listForm)
		{
			base.EntityToControl(clsEntity, listForm);

			clsControls.SetAllColumnsToCheckBoxList(cblColumn, ((Entity)clsEntity).ColumnIDs);
			clsControls.SetNoInheritWebColumnsToCheckBoxList(cblWebColumn, ((Entity)clsEntity).WebColumnIDs);
		}

		/// <summary>
		/// 根据栏目ID，监测他上级是否还有栏目，有的话把上级栏目ID加到权限字符串中
		/// </summary>
		/// <![CDATA[2014-8-10 11:17:50]]>
		/// <param name="nColumnID"></param>
		/// <param name="strRight"></param>
		private void SelectFather(int nColumnID, ref string strRight)
		{
			Column.Entity clsColumn;

			//clsColumn = clsColumnCom.GetOneByID(nColumnID);
			clsColumn = clsColumnComp.GetOne(nColumnID);
			if (clsColumn.ParentID != clsConfig.nColumn_NoParent)
			{
				if (TonyNet4V1.CommonFunction.FindString(strRight, clsColumn.ParentID.ToString()))
				{

				}
				else
				{
					strRight = strRight + clsColumn.ParentID.ToString() + ", ";
				}
				SelectFather(clsColumn.ParentID, ref strRight);
			}
		}

		/// <summary>
		/// 根据前台栏目ID，监测他上级是否还有栏目，有的话把上级栏目ID加到权限字符串中
		/// </summary>
		/// <![CDATA[2014-8-13 17:52:27]]>
		/// <param name="nColumnID"></param>
		/// <param name="strRight"></param>
		private void SelectWebFather(int nColumnID, ref string strRight)
		{
			WebColumn.Entity clsWebColumn;

			//clsColumn = clsColumnCom.GetOneByID(nColumnID);
			clsWebColumn = clsWebColumnComp.GetOne(nColumnID);
			if (clsWebColumn.ParentID != clsConfig.nColumn_NoParent)
			{
				if (TonyNet4V1.CommonFunction.FindString(strRight, clsWebColumn.ParentID.ToString()))
				{

				}
				else
				{
					strRight = strRight + clsWebColumn.ParentID.ToString() + ", ";
				}
				SelectWebFather(clsWebColumn.ParentID, ref strRight);
			}
		}

	}
}
